using System;
using System.Collections.Generic;
using System.Linq;
using MNH.Yarekoodak.DomainClasses;
using MNH.WebApps.Common.Data;
using MNH.Yarekoodak.DomainClasses.Repositories;

namespace MNH.Yarekoodak.Services.Locations
{
    /// <summary>
    /// Location service
    /// </summary>
    public partial class LocationService : ILocationService
    {
        #region Fields
        private readonly IRepository<Location> _locationRepository = new EfRepository<Location>();
        #endregion

        #region Ctor
        public LocationService()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a location 
        /// </summary>
        /// <param name="location">Location </param>
        public virtual void DeleteLocation(Location location)
        {
            if (location == null)
                throw new ArgumentNullException("location");

            _locationRepository.Delete(location);
        }

        /// <summary>
        /// Gets a location 
        /// </summary>
        /// <param name="locationId">Location  identifier</param>
        /// <returns>Location </returns>
        public virtual Location GetLocationById(int locationId, bool asNoTrack = false)
        {
            if (locationId == 0)
                return null;

            return _locationRepository.Table.Where(c => c.Id == locationId).SingleOrDefault();
        }

        /// <summary>
        /// Gets all location s
        /// </summary>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Location s</returns>
        public virtual IPagedList<Location> GetAllLocations(
            DateTime? dateFrom, DateTime? dateTo, int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _locationRepository.Table;
            query = query.OrderBy(b => b.Title);

            var locations = new PagedList<Location>(query, pageIndex, pageSize);
            return locations;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Location> GetAllLocations()
        {
            var locations = _locationRepository.Table;
            locations = locations.OrderBy(b => b.Title);
            return locations;
        }

        /// <summary>
        /// Inserts an location 
        /// </summary>
        /// <param name="location">Location </param>
        public virtual void InsertLocation(Location location)
        {
            if (location == null)
                throw new ArgumentNullException("location");

            _locationRepository.Insert(location);
        }

        /// <summary>
        /// Updates the location 
        /// </summary>
        /// <param name="location">Location </param>
        public virtual void UpdateLocation(Location location)
        {
            if (location == null)
                throw new ArgumentNullException("location");

            _locationRepository.Update(location);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Location> GetAllStates()
        {
            return _locationRepository.Table.Where(c => c.ParentId == null).OrderBy(c => c.Title);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stateId"></param>
        /// <returns></returns>
        public IEnumerable<Location> GetAllCitiesByStateId(int stateId)
        {
            if (stateId == 0)
            {
                return _locationRepository.Table.Where(c => c.Level == 2).OrderBy(c => c.Title);
            }
            return _locationRepository.Table.Where(c => c.ParentId == stateId).OrderBy(c => c.Title);
        }

        public Location GetCapitalByStateId(int stateId)
        {
            return _locationRepository.Table.FirstOrDefault(c => c.IsCapital && c.ParentId == stateId);
        }
        #endregion
    }
}
